package com.ef.web.action;

import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;

import com.ef.dao.ContractDao;
import com.ef.domain.User;
import com.ef.web.domain.RevenueCount;


public class DepositByCCAction extends ReportAction {

	private static final long serialVersionUID = 1L;

	private ContractDao contractDao;
	
	private Date start;
	private Date end;
	private Date lastStart;
	private Date lastEnd;
	
	private List<Object> object;
	
	private List<RevenueCount> result;
	 
	public String list(){
		
		if(start==null){
			start = new Date();
		}
		if(end==null){
			end =new Date();
		}
		
		SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
		String _start = formater.format(start);
		int _start_year = Integer.valueOf(_start.substring(0, 4));
		String _end = formater.format(end);
		int _end_year = Integer.valueOf(_end.substring(0, 4));
		Date lastStart = null;
		Date lastEnd = null;
		try {
		  lastStart =	formater.parse((_start_year-1)+_start.substring(4));
		  lastEnd = formater.parse((_end_year-1)+_end.substring(4));
		} catch (ParseException e) {
			 
			e.printStackTrace();
		}
		this.setLastStart(lastStart);
		this.setLastEnd(lastEnd);
		User user = getSessionUser();
		result = contractDao.depositByCC(start,end, user.getSchool().getId(), null);
		 
		return LIST;
	}
	
	public void xml(){
		
		if(start==null){
			start = new Date();
		}
		if(end==null){
			end =new Date();
		}
		
		SimpleDateFormat formater = new SimpleDateFormat("yyyyMMdd");
		String _start = formater.format(start);
		int _start_year = Integer.valueOf(_start.substring(0, 4));
		String _end = formater.format(end);
		int _end_year = Integer.valueOf(_end.substring(0, 4));
		Date lastStart = null;
		Date lastEnd = null;
		try {
		  lastStart =	formater.parse((_start_year-1)+_start.substring(4));
		  lastEnd = formater.parse((_end_year-1)+_end.substring(4));
		} catch (ParseException e) {
			 
			e.printStackTrace();
		}
		this.setLastStart(lastStart);
		this.setLastEnd(lastEnd);
		User user = getSessionUser();
		result = contractDao.depositByCC(start,end,user.getSchool().getId(),  null);
		 
		Element chart = DocumentFactory.getInstance().createElement("chart");
		Element series = DocumentFactory.getInstance().createElement("series");
		Element graphs = DocumentFactory.getInstance().createElement("graphs");
		Element graph1 = DocumentFactory.getInstance().createElement("graph");
		chart.add(series);
		chart.add(graphs);
		graph1.addAttribute("gid", "1");
		Element graph2 = DocumentFactory.getInstance().createElement("graph");
		graph2.addAttribute("gid", "0");
		graphs.add(graph1);
		graphs.add(graph2);
		int xId = 0;
		for(RevenueCount rc:result) {
			Element value = DocumentFactory.getInstance().createElement("value");
			value.addAttribute("xid", String.valueOf(xId));
			value.addText(rc.getTitle());
			Element g1Value = DocumentFactory.getInstance().createElement("value");
			g1Value.addAttribute("xid", String.valueOf(xId));
			g1Value.addText(String.valueOf(rc.getCurrentSum()));
			Element g2Value = DocumentFactory.getInstance().createElement("value");
			g2Value.addAttribute("xid", String.valueOf(xId++));
			g2Value.addText(String.valueOf(rc.getLastSum()));
			series.add(value);
			graph1.add(g1Value);
			graph2.add(g2Value);
		}
		
		try {
			HttpServletResponse httpResponse = ServletActionContext.getResponse();
			httpResponse.setContentType("text/xml");
			httpResponse.setCharacterEncoding("UTF-8");
			PrintWriter out = httpResponse.getWriter();
			out.write(chart.asXML());
		} catch (Exception ignore) {
			ignore.printStackTrace();
		}
		return;
	}

	public String export(){
		list();
		SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("doc.filename", "Deposit By CC");
		map.put("doc.title", "CC");
		map.put("doc.new", "From ("+formater.format(start)+") To ("+formater.format(end)+" )");
		map.put("doc.old", "From ("+formater.format(lastStart)+") To ("+formater.format(lastEnd)+" )");
		map.put("doc.data", result);
		exportExcel(getResponse(),map
				);
		
		return NONE;
	}
	
	public ContractDao getContractDao() {
		return contractDao;
	}

	public void setContractDao(ContractDao contractDao) {
		this.contractDao = contractDao;
	}

	public List<Object> getObject() {
		return object;
	}

	public void setObject(List<Object> object) {
		this.object = object;
	}

	public Date getStart() {
		return start;
	}

	public void setStart(Date start) {
		this.start = start;
	}

	public Date getEnd() {
		return end;
	}

	public void setEnd(Date end) {
		this.end = end;
	}

	public List<RevenueCount> getResult() {
		return result;
	}

	public void setResult(List<RevenueCount> result) {
		this.result = result;
	}

	public Date getLastStart() {
		return lastStart;
	}

	public void setLastStart(Date lastStart) {
		this.lastStart = lastStart;
	}

	public Date getLastEnd() {
		return lastEnd;
	}

	public void setLastEnd(Date lastEnd) {
		this.lastEnd = lastEnd;
	}
	
	

}
